// When an item or a file is modified //
// trigger List: ContractList

/* --- trigger conditions ---

@greater(triggerOutputs()?['body/ID'], 100)
@equals(triggerOutputs()?['body/ApprovalResult'], 'Returned')

--- trigger conditions --- */


// 表服务
var List_ContractList = {}
var List_ApprovalData = {}
var List_Approver = {}
var List_Department = {}
var List_OtherServicesDepartment = {}


var Get_item_申请表 = List_ContractList.getItem(id=triggerOutputs()['body/ID'])

var Round = float(triggerOutputs()['body/ApprovalBatch'])

if(triggerOutputs()['body/ApprovalNode'] == "LegalFinance")
{
    Round += 1
}

// 设置审批人为空
List_ContractList.update(id=Get_item_申请表['body/ID'],{
    "item/InvolveMonetary": true,
    "item/LegalInvolved": true,
    "item/ApprovalUser": null,
    "item/ApprovalResult": null,
    "item/ApprovalNode": triggerOutputs()['body/ApprovalNode']+"Rejected",
    "item/ApprovalRemark": null,
    "item/ApprovalBatch": Round
})


// 获取数据，处理数据

var Get_items = List_ApprovalData
    .getItems(`ContractId eq '${Get_item_申请表['body/ID']}' and ApprovalNode eq '${Get_item_申请表['body/ApprovalNode']}'   and result eq null and batch_no eq ${triggerOutputs()['body/ApprovalBatch']}`)
    .orderBy('ID asc')

var 筛选数组 = Get_items['body/value'].where(x => x['AllApprover'].contains(triggerOutputs()['body/Editor']))

// Schema see: Schema.json
var 分析_JSON =  筛选数组[0]

var 选择_2 = 分析_JSON['AllApprover'].select(x => x['Claims'])

// 更新当前approvedata数据
List_ApprovalData.update(id=分析_JSON['ID'],{
    "item/Title": null,
    "item/Approver/Claims": Get_item_申请表['body/Editor/Claims'],
    "item/is_completed": true,
    "item/result": triggerOutputs()['body/ApprovalResult'],
    "item/remark": Get_item_申请表['body/ApprovalRemark'],
    "item/CompletedDate": utcNow(),
    "item/is_validate": true,
    "item/is_revoked": false,
    "item/is_backup": false
})

